package com.oa.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpRequest;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.oa.bean.LeaSec;
import com.oa.bean.Leave;
import com.oa.service.impl.PMServiceImpl;

@Controller
@RequestMapping("/show")
public class PersonManage {
	
	@Autowired
	private PMServiceImpl pMServiceImpl;

	@RequestMapping("/showLea")
	@ResponseBody
	public Map<String, Object> showLea(String usp,Integer currentPage){
		System.out.println(usp);
		if(currentPage==0){
			currentPage=1;
		}
		System.out.println("currentPage="+currentPage);
		Page<PageInfo> pageInfo=PageHelper.startPage(currentPage, 8,"l_id desc");
		
		List<Leave> leaves=pMServiceImpl.findLea(usp);
		System.out.println(leaves);
		Map<String, Object> map=new HashMap<>();
		map.put("leaList", leaves);
		map.put("pages", pageInfo.getPages());
		return map;
	}
	
	@RequestMapping("/deleteLea")
	public String deleteLea(int l_id){
		int a=pMServiceImpl.deleteLea(l_id);
		System.out.println(a);
		if (a!=0) {
			return "../Template/showLeave";
		}
		return null;
	}
	
	@RequestMapping("/findlea")
	public String findlea(int l_id,Model model){
		LeaSec leave=pMServiceImpl.findlea(l_id);
		System.out.println(leave);
		
		model.addAttribute("leave", leave);
		return "../Template/exapp";
	}
	
	@RequestMapping("/showLeave")
	public String findLeave(int l_id,Model model){
		LeaSec leave=pMServiceImpl.findlea(l_id);
		System.out.println(leave.getL_status());
		//判断状态
		String status=null;
		if (leave.getL_status()==0) {
			status="未审批";
		}else if (leave.getL_status()==1) {
			status="审批通过";
		}else {
			status="审批未通过";
		}
		model.addAttribute("leave", leave);
		model.addAttribute("status", status);
		return "../Template/showLea";
	}
	
	@RequestMapping("/updateLea")
	public String updateLea(Model model,HttpServletRequest request){
		String btn=request.getParameter("btn");
		System.out.println(btn);
		int l_id=Integer.parseInt(request.getParameter("hid"));
		System.out.println(l_id);
		int status=0;
		if (btn.equals("审批通过")) {
			status=1;
		}else if(btn.equals("审批不通过")){
			status=2;
		}
		int result=pMServiceImpl.updataLea(status, l_id);
		if (result!=0) {
			return "../Template/showLeave";
		}else{
			return "../Template/exapp";
		}
		
	}
	
	//删除  批量删除
	@RequestMapping("/deleteMore")
	@ResponseBody
	public Map<String,Object> deleteMore(String str){	
		String[] ptids=str.split(",");
		System.out.println(ptids);
		Map<String, Object> map=new HashMap<>();
		for (int i = 0; i < ptids.length; i++) {
			System.out.println(ptids[i]);
			int l_id=Integer.parseInt(ptids[i]);
			int result=pMServiceImpl.deleteLea(l_id);
			if(result==1){				
				map.put("map1", result);
			}
		}
		return map;	
	}
	
}
